package com.bbms.controller.reader;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bbms.entity.ErrorCode;
import com.bbms.entity.Page;
import com.bbms.entity.Reader;
import com.bbms.entity.Result;
import com.bbms.entity.SuccessCode;
import com.bbms.entity.User;
import com.bbms.service.ReaderService;
import com.bbms.util.StringUtil;
/**
 * @author Administrator
 * 读者管理控制器
 */
@Controller
@RequestMapping("/bbms/readerManage")
public class ReaderManageController {
	
	@Resource(name="ReaderServiceImpl")
	private ReaderService rs;
	
	@RequestMapping("/readerManage")
	String readerManage(){
		return "readerManage/readerManage.html";
	}
	
	/**
	 * 获取分页数据
	 * @param page 当前页
	 * @param rows 数量
	 * @param s_name 用户名
	 * @param s_account 用户账号
	 * @param s_phone 电话
	 * @param s_categoryId 分类
	 * @param IDCard 身份证
	 * @param start_time 开始时间
	 * @param end_time 结束时间
	 * @return 分页数据
	 */
	@ResponseBody
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public Page getPage(int page, int rows, String s_name, String s_account, String s_phone,
			    Integer s_categoryId, String IDCard, String start_time, String end_time, Integer endTime) {
		
		System.out.println("参数： "+s_name + s_phone + s_categoryId + IDCard + start_time + end_time+ endTime);
		
		Page data=rs.getPage(page, rows, s_name, s_account, s_phone, s_categoryId, 
														IDCard, start_time, end_time, endTime);
		
		return data;
	}
	
	
	/**
	 * 添加/修改读者 
	 * @return 反馈信息
	 */
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public Result insert(@ModelAttribute("reader")Reader reader,@ModelAttribute("user")User user){
		Result result=new Result();

			if (reader.getId()==0) {

				try {
					rs.saveTwoObj(reader, user);
					result.setStatus(SuccessCode.insertSuccess.getCode());
					result.setMsg(SuccessCode.insertSuccess.getName());
				} catch (Exception e) {
					result.setStatus(ErrorCode.insertFailed.getCode());
					result.setMsg(ErrorCode.insertFailed.getName());
				}
				
			}else {
				
				try {
					rs.updateTwoObj(reader, user);
					result.setMsg(SuccessCode.updateSuccess.getName());
					result.setStatus(SuccessCode.updateSuccess.getCode());
				} catch (Exception e) {
					e.printStackTrace();
					result.setStatus(ErrorCode.updateFailed.getCode());
					result.setMsg(ErrorCode.updateFailed.getName());
				}
				
			}
			
		return result;
	}
	
	
	/**
	 * 删除两个实体
	 * @param reader 读者
	 * @param user 用户
	 * @return 反馈信息
	 */
	@ResponseBody
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public Result delete(@ModelAttribute("reader")Reader reader,@ModelAttribute("user")User user){
		Result result=new Result();
		
		try {
			
			rs.delTwoObj(reader, user);
			result.setStatus(SuccessCode.deleteSuccess.getCode());
			result.setMsg(SuccessCode.deleteSuccess.getName());
			
		} catch (Exception e) {
			
			e.printStackTrace();
			result.setStatus(ErrorCode.readerDeleteFailed.getCode());
			result.setMsg(ErrorCode.readerDeleteFailed.getName());
		}
		
		return result;
	}
	
	/**
	 * 读者批量删除
	 * @param ids
	 * @param password
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="bacthDel", method=RequestMethod.POST)
	public Result bacthDel(String ids, String password, HttpServletRequest request){
		Result result=new Result();
		HttpSession session=request.getSession();
		String managerAccount=(String) session.getAttribute("user");	//当前管理员账号
				
		String[] strings=ids.split(",");
		List<Integer> list=new ArrayList<Integer>();
		
		for (int i = 0; i < strings.length; i++) {
			if (StringUtil.isNotEmpty(strings[i])){
				list.add(Integer.parseInt(strings[i]));
			}
		}	

		try {
			
			if (rs.bacthDel(list, password, managerAccount)) {
				
				result.setStatus(SuccessCode.deleteSuccess.getCode());
				result.setMsg(SuccessCode.deleteSuccess.getName());
			}else {
				
				result.setStatus(ErrorCode.managerFaild.getCode());
				result.setMsg(ErrorCode.managerFaild.getName());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(ErrorCode.readerBatchDeleteFailed.getCode());
			result.setMsg(ErrorCode.readerBatchDeleteFailed.getName());
		}
	
		return result;
	}
	
}
